/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlterarSenha.java
 *
 * Created on 16/09/2010, 17:49:15
 */
package NajaTaskClient.interfaces.comum;

import NajaTaskClient.Conexao.Configuracoes;
import javax.swing.JOptionPane;

/**
 *
 * @author alex.silva
 */
public class configurarConexao extends javax.swing.JDialog {

    /** Creates new form AlterarSenha */
    public configurarConexao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(getRootPane());
        carregaConfig();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelAlterarSenha = new javax.swing.JPanel();
        labelSenhaAtual = new javax.swing.JLabel();
        textfieldHostname = new javax.swing.JTextField();
        labelSenhaAtual1 = new javax.swing.JLabel();
        textfieldPort = new javax.swing.JTextField();
        buttonSalvar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        textfieldUsuario = new javax.swing.JTextField();
        labelSenhaAtual2 = new javax.swing.JLabel();
        labelSenhaAtual3 = new javax.swing.JLabel();
        textfieldSenha = new javax.swing.JTextField();
        labelSenhaAtual4 = new javax.swing.JLabel();
        textfieldBancoDados = new javax.swing.JTextField();
        buttonRestaurar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        labelSenhaAtual.setText("Host");
        labelSenhaAtual.setToolTipText("Nome ou endereço IP do servidor MySQL(padrão: localhost)");

        textfieldHostname.setToolTipText("Nome ou endereço IP do servidor MySQL(padrão: localhost)");

        labelSenhaAtual1.setText("Porta");
        labelSenhaAtual1.setToolTipText("Porta TCP/IP (padrão: 3306)");

        textfieldPort.setToolTipText("Porta TCP/IP (padrão: 3306)");

        buttonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/accept.png"))); // NOI18N
        buttonSalvar.setText("Aplicar");
        buttonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSalvarActionPerformed(evt);
            }
        });

        buttonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/cancel.png"))); // NOI18N
        buttonCancelar.setText("Cancelar");
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        textfieldUsuario.setToolTipText("Nome de usuário para a conexão (padrão: root)");

        labelSenhaAtual2.setText("Usuário");
        labelSenhaAtual2.setToolTipText("Nome de usuário para a conexão (padrão: root)");

        labelSenhaAtual3.setText("Senha");
        labelSenhaAtual3.setToolTipText("Senha do banco de dados (padrão: em branco)");

        textfieldSenha.setToolTipText("Senha do banco de dados (padrão: em branco)");

        labelSenhaAtual4.setText("Banco de dados");
        labelSenhaAtual4.setToolTipText("Nome do banco de dados (padrao: najatask)");

        textfieldBancoDados.setToolTipText("Nome do banco de dados (padrao: najatask)");

        buttonRestaurar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/cog_edit.png"))); // NOI18N
        buttonRestaurar.setText("Restaurar padrão");
        buttonRestaurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRestaurarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelAlterarSenhaLayout = new javax.swing.GroupLayout(panelAlterarSenha);
        panelAlterarSenha.setLayout(panelAlterarSenhaLayout);
        panelAlterarSenhaLayout.setHorizontalGroup(
            panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                        .addComponent(labelSenhaAtual2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                    .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                        .addComponent(labelSenhaAtual)
                        .addGap(18, 18, 18)
                        .addComponent(textfieldHostname, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelSenhaAtual1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                        .addComponent(labelSenhaAtual3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(textfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                        .addComponent(labelSenhaAtual4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAlterarSenhaLayout.createSequentialGroup()
                                .addComponent(buttonCancelar)
                                .addGap(18, 18, 18)
                                .addComponent(buttonRestaurar)
                                .addGap(18, 18, 18)
                                .addComponent(buttonSalvar))
                            .addComponent(textfieldBancoDados, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))))
                .addContainerGap())
        );
        panelAlterarSenhaLayout.setVerticalGroup(
            panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAlterarSenhaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenhaAtual)
                    .addComponent(textfieldHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textfieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSenhaAtual1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenhaAtual2)
                    .addComponent(textfieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textfieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSenhaAtual3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textfieldBancoDados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSenhaAtual4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelAlterarSenhaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSalvar)
                    .addComponent(buttonRestaurar)
                    .addComponent(buttonCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelAlterarSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelAlterarSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSalvarActionPerformed
        this.salvaConfig();
}//GEN-LAST:event_buttonSalvarActionPerformed

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_buttonCancelarActionPerformed

    private void buttonRestaurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRestaurarActionPerformed
       if(Configuracoes.get().restaurarPadrao()){
           this.carregaConfig();
           JOptionPane.showMessageDialog(this, "Configurações restauradas com sucesso");
       }
    }//GEN-LAST:event_buttonRestaurarActionPerformed

    private void carregaConfig() {
        this.textfieldHostname.setText(Configuracoes.get().obterConfig().obterDbHost());
        this.textfieldUsuario.setText(Configuracoes.get().obterConfig().obterDbUser());
        this.textfieldSenha.setText(Configuracoes.get().obterConfig().obterDbPass());
        this.textfieldPort.setText(Configuracoes.get().obterConfig().obterDbPort());
        this.textfieldBancoDados.setText(Configuracoes.get().obterConfig().obterDb());
    }

    private void salvaConfig() {
        Configuracoes.get().obterConfig().setDbHost(this.textfieldHostname.getText());
        Configuracoes.get().obterConfig().setDbUser(this.textfieldUsuario.getText());
        Configuracoes.get().obterConfig().setDbPass(this.textfieldSenha.getText());
        Configuracoes.get().obterConfig().setDbPort(this.textfieldPort.getText());
        Configuracoes.get().obterConfig().setDb(this.textfieldBancoDados.getText());
        if (Configuracoes.get().salvaConfig()) {
            JOptionPane.showMessageDialog(this, "Alterações salvas com sucesso");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Erro ao salvar as alterações");
        }



    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                configurarConexao dialog = new configurarConexao(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);


            }
        });
    }

    private void alterarSenha() {
        //TODO: criar a funçao que altera o nome de login
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JButton buttonRestaurar;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JLabel labelSenhaAtual;
    private javax.swing.JLabel labelSenhaAtual1;
    private javax.swing.JLabel labelSenhaAtual2;
    private javax.swing.JLabel labelSenhaAtual3;
    private javax.swing.JLabel labelSenhaAtual4;
    private javax.swing.JPanel panelAlterarSenha;
    private javax.swing.JTextField textfieldBancoDados;
    private javax.swing.JTextField textfieldHostname;
    private javax.swing.JTextField textfieldPort;
    private javax.swing.JTextField textfieldSenha;
    private javax.swing.JTextField textfieldUsuario;
    // End of variables declaration//GEN-END:variables
}
